package com.topscomm.gpm.script;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.topscomm.gpm.pojo.*;
import com.topscomm.gpm.service.impl.*;
import org.springframework.transaction.annotation.Transactional;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.gpm.GpmScript;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;

public class GpmProjectTaskFeedbackScript extends GpmScript {

	/**
	 * 任务反馈结束后更新对应任务相关字段，对应里程碑字段，对应项目字段
	 *
	 * @param service
	 * @param entity
	 * @throws Exception
	 */
	@Transactional
	public void projectTaskUpdate(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmProjectTaskFeedbackService gpmProjectTaskFeedbackService = (GpmProjectTaskFeedbackService) SpringUtil
				.getBean("gpmProjectTaskFeedbackService");
		GpmProjectTaskService gpmProjectTaskService = (GpmProjectTaskService) SpringUtil
				.getBean("gpmProjectTaskService");
		GpmProjectMilepostService gpmProjectMilepostService = (GpmProjectMilepostService) SpringUtil
				.getBean("gpmProjectMilepostService");
		GpmProjectPlanService gpmProjectPlanService = (GpmProjectPlanService) SpringUtil
				.getBean("gpmProjectPlanService");
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil
				.getBean("gpmProjectService");
		// 查询项目任务反馈所属任务
		GpmProjectTaskFeedbackEntity gpmProjectTaskFeedbackEntity = gpmProjectTaskFeedbackService.queryById(sourceid);
		String taskid = gpmProjectTaskFeedbackEntity.getTaskid();
		GpmProjectTaskEntity gpmProjectTaskEntity = gpmProjectTaskService.queryById(taskid);
		// 更新项目任务字段
		String taskstate = String.valueOf(gpmProjectTaskFeedbackEntity.getTaskstate());
		String taskschedule = gpmProjectTaskFeedbackEntity.getTaskschedule();
		gpmProjectTaskEntity.setTaskstate(taskstate);
		gpmProjectTaskEntity.setTaskschedule(taskschedule);
		gpmProjectTaskEntity.setActualenddate(gpmProjectTaskFeedbackEntity.getTaskactualenddate());
		gpmProjectTaskService.updateEntity(gpmProjectTaskEntity);
		// 判断是否需更新里程碑
		if ("2".equals(gpmProjectTaskFeedbackEntity.getTaskstate())) {
			boolean isFinish = true;
			String milepostcode = gpmProjectTaskEntity.getMilepostcode();
			StringBufferProxy whereSql = new StringBufferProxy();
			whereSql.appendLineSingle("milepostcode = {0} and enabled = 1", milepostcode);
			List<GpmProjectTaskEntity> sdProjectTaskEntities = gpmProjectTaskService.queryByWhere(whereSql.toString());
			for (GpmProjectTaskEntity gpmProjectTaskEntity2 : sdProjectTaskEntities) {
                if (!"2".equals(gpmProjectTaskEntity2.getTaskstate()) && !"4".equals(gpmProjectTaskEntity2.getTaskstate())) {
                    isFinish = false;
                    break;
                }
			}
			// 全部完成或者关闭，回写里程碑
			if (isFinish) {
				whereSql.clear();
				whereSql.appendLineSingle("operatecode = {0} and enabled = 1 ", milepostcode);
				List<GpmProjectMilepostEntity> sdProjectMilepostEntities = gpmProjectMilepostService
						.queryByWhere(whereSql.toString());
				for (GpmProjectMilepostEntity gpmProjectMilepostEntity : sdProjectMilepostEntities) {
					gpmProjectMilepostEntity.setMilepoststate(1);
					gpmProjectMilepostEntity.setActualfinishtime(new Date());
					gpmProjectMilepostService.updateEntity(gpmProjectMilepostEntity);
				}
				// 查询该任务对应的项目,判断是否需要更新项目状态至复盘阶段
				GpmProjectEntity gpmProjectEntity = gpmProjectService.queryById(ConvertUtil.convertToString(gpmProjectTaskEntity.getProjectid()));
				if(!ObjectUtil.isEmpty(gpmProjectEntity)) {
					gpmProjectTaskService.queryMilepostAllFinished(gpmProjectEntity);
				}
			}
		}

	}
}
